package com.lsj.gulimall.product.app.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.lsj.gulimall.product.service.AttrAttrgroupRelationService;
import com.lsj.gulimall.product.vo.AttrGroupRelationVo;
import com.lsj.gulimall.product.vo.AttrGroupWithAttrsVo;
import com.lsj.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lsj.gulimall.product.entity.AttrGroupEntity;
import com.lsj.gulimall.product.service.AttrGroupService;
import com.lsj.common.utils.PageUtils;
import com.lsj.common.utils.R;



/**
 * 属性分组
 *
 * @author lsj
 * @email 1326646663@qq.com
 * @date 2022-01-06 11:30:26
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
        AttrGroupEntity attrGroup = attrGroupService.getInfoById(attrGroupId);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 获取属性分组下的关联属性列表
     * @param attrGroupId
     * @return
     */
    @GetMapping("/{attrGroupId}/attr/relation")
    public R getGroupAttrRelation(@RequestParam Map<String, Object> params,@PathVariable("attrGroupId") Long attrGroupId){
        List<AttrVo> attrVos = attrGroupService.getGroupAttrRelation(attrGroupId);
        return R.ok().put("data",attrVos);
    }

    /**
     * 移除属性分组关联
     * @param ids
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] ids){
        attrGroupService.deleteRelation(ids);
        return R.ok();
    }


    /**
     * 获取属性分组没有关联的其他属性
     * @param params
     * @param attrgroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getNoRelationAttr(@RequestParam Map<String,Object> params,@PathVariable("attrgroupId") Long attrgroupId){
        PageUtils page = attrGroupService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page",page);
    }


    @PostMapping("/attr/relation")
    public R addGroupAttrRelation(@RequestBody AttrGroupRelationVo[] attrGroupRelationVo) {
        attrAttrgroupRelationService.addGroupAttrRelation(attrGroupRelationVo);
        return R.ok();
    }

    // /product/attrgroup/{catelogId}/withattr

    @GetMapping("/{catelogId}/withattr")
    public R getGroupWithAttr(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getGroupWithAttr(catelogId);
        return R.ok().put("data",vos);
    }


}
